 <?php 

 class members extends CI_Controller {
     
 
 	public function index() {
 		//dashboard
 		if($this->is_logged_in())
 		{
            $this->load->model('adModel');
            $data['records'] = $this->adModel->get_categories();
            //loading views        
            $this->load->view('header');
            $this->load->view('static_nav');
            $this->load->view('dashboard_content',$data);
    		$this->load->view('footer');
 		}
 		
 	}

 	public function is_logged_in() {
 		//login validation
 		$flag = $this->session->userdata('logged_in');
 		if($flag != true) 
 		{
 			$this->load->view('header');
 			$this->load->view('static_nav');
 			$this->load->view('not_logged_in');
 			$this->load->view('footer');
 		}
 		return $flag;
 	}

 	public function Upload_notes()
	{
        if($this->is_logged_in())
        {
            //upload notes
    		$this->load->view('header');
    		$this->load->view('static_nav');
    		$this->load->view('up_notes_content');
    		$this->load->view('footer');
        }
	}

	public function Upload_ads()
	{
        if($this->is_logged_in())
        {
            $this->load->model('adModel');
            $data['records'] = $this->adModel->get_categories();
            //upload ads
    		$this->load->view('header');
    		$this->load->view('static_nav');
    		$this->load->view('up_ads_content',$data);
    		$this->load->view('footer');
        }
	}

	public function create_new_note()
    {
    	//create new note
        
        //upload file part  
        if($this->is_logged_in())
        {
        //    $this->upload->set_allowed_types('*');

		

        //file uplaod code
        $config['upload_path'] = './Assets/img/uploads/notes/';
	    $config['allowed_types'] = '*';
		$this->load->library('upload', $config);
        $this->upload->initialize($config);
        $this->upload->set_allowed_types('*');
		$data['upload_data'] = '';
		if (!$this->upload->do_upload('userfile')) {
		  echo "hello" ;
            $data = array('msg' => $this->upload->display_errors());
		}
        else
        { //else, set the success message
			$data = array('msg' => "Upload success!");
            $data['upload_data'] = $this->upload->data();
           //getting file path 
            $file_name = '';
            $file_name .=base_url();
            $file_name .="/Assets/img/uploads/notes/";
            $file_name .=$data['upload_data']['file_name'];
        }
        
        
        
        
            
            
            //rest of the information
            
          
            $data = array(
                     'user_id' => $this->session->userdata('user_id'),
                    'note_title' => $this->input->post('title'), 
                    'faculty' => $this->input->post('faculty'), 
                    'semester' =>$this->input->post('semester'), 
                    'author' => $this->input->post('teacher'),
                    'softcopy'=>$file_name,
                    'disc'=>$this->input->post('discription'),
                    'active'=>'1'
    );

             $this->load->model('upload_notes');
             $this->upload_notes->upload($data);
             $this->index();
         }
    }

    private function upload_file_ad($i){

            $config['upload_path'] = './Assets/img/uploads/ads/';
            $config['allowed_types'] = 'gif|jpg|png';
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            $this->upload->set_allowed_types('*');
            $data['upload_data'] = '';
            if (!$this->upload->do_upload('img'.$i)) {
                $data = array('msg' => $this->upload->display_errors());
            }
            else
            { //else, set the success message
                $data = array('msg' => "Upload success!");
                $data['upload_data'] = $this->upload->data();
               //getting file path 
                $file_name = '';
                $file_name .=base_url();
                $file_name .="/Assets/img/uploads/ads/";
                $file_name .=$data['upload_data']['file_name'];
                return $file_name;
            } 

    }

    public function create_new_ad()
    {
        if($this->is_logged_in())
        {
            //uplaoding images
            $name = array();
            for($i=1;$i<=3;$i++)
            {
                if (!empty($_FILES['img'.$i]['name'])) 
                $name[$i] = $this->upload_file_ad($i);
                else
                $name[$i] = base_url()."/Assets/img/default.jpg";    
            }
                
        	//create new ad
            $data = array(  'cat_id' => $this->input->post('cat'),
                            'user_id' => $this->session->userdata('user_id'),
                            'ad_title' => $this->input->post('title'),
                            'price' => $this->input->post('price'),
                            'disc' => $this->input->post('disc'),
                            'img_1' => $name[1],
                            'img_2' => $name[2],
                            'img_3' => $name[3],
                            'keywords' => $this->input->post('keywords'),
                            'contact' => $this->input->post('contact'),
                            'active' => 2,
                            'views' => 0
                );

            $this->load->model('adModel');
            $this->adModel->insert_new_ad($data);
            $this->index();
        }
    }

    public function my_ads()
    {
        if($this->is_logged_in())
        {
            $email=$this->session->userdata('email');
            $id=$this->session->userdata('user_id');

            $this->load->library('pagination'); 
            $this->load->model('my_ads');
            //configuring pagination
            $config['base_url'] = base_url()."/members/my_ads";
            $config['per_page'] = 3;
            $config['num_links'] = 5;

            $config['full_tag_open'] = '<nav> <ul class="pagination">';
            $config['full_tag_close'] = '</ul> </nav>';

            $config['num_tag_open'] = '<li>';
            $config['num_tag_close'] = '</li>';

            $config['cur_tag_open'] = '<li class="active"><a href="#">';
            $config['cur_tag_close'] = '</a></li>';

            $config['prev_link'] = '&laquo;';
            $config['prev_tag_open'] = '<li>';
            $config['prev_tag_close'] = '</li>';

            $config['next_link'] = '&raquo;';
            $config['next_tag_open'] = '<li>';
            $config['next_tag_close'] = '</li>';
            
            $this->db->where('user_id',$id);
            $config['total_rows'] = $this->db->get('ads')->num_rows();
            
            //init pagination
            $this->pagination->initialize($config);

            //getting data from db
            $data['records'] = $this->my_ads->get_ads($config['per_page'], $this->uri->segment(3),$email);


        	$this->load->view('header');
    		$this->load->view('static_nav');
    		$this->load->view('my_ads_content',$data);
    		$this->load->view('footer');
        }
    }
     
    public function my_notes()
    {
        if($this->is_logged_in())
        {
            $email=$this->session->userdata('email');
            
                   
            $this->load->library('pagination'); 
            $this->load->model('upload_notes');
            //configuring pagination
            $config['base_url'] = base_url()."members/my_notes";
            $config['per_page'] = 3;
            $config['num_links'] = 4;
            $config['uri_segment'] = 3;
            $config['full_tag_open'] = '<nav> <ul class="pagination">';
            $config['full_tag_close'] = '</ul> </nav>';

            $config['num_tag_open'] = '<li>';
            $config['num_tag_close'] = '</li>';

            $config['cur_tag_open'] = '<li class="active"><a href="#">';
            $config['cur_tag_close'] = '</a></li>';

            $config['prev_link'] = '&laquo;';
            $config['prev_tag_open'] = '<li>';
            $config['prev_tag_close'] = '</li>';

            $config['next_link'] = '&raquo;';
            $config['next_tag_open'] = '<li>';
            $config['next_tag_close'] = '</li>';
             $data = $this->upload_notes->get_notes($config['per_page'], $this->uri->segment(3),$email);
            
            $config['total_rows'] = $data['num_rows'];
            
            //init pagination
            $this->pagination->initialize($config);

            //getting data from db
           


           
            
        	$this->load->view('header');
    		$this->load->view('static_nav');
    		$this->load->view('my_notes_content',$data);
    		$this->load->view('footer');
        }
    }

     
     public function edit_notes()
    {
        $id=$this->input->post('notes_id');
         echo $id;
        $this->load->model('upload_notes');
        $data['records'] = $this->upload_notes->get_edit_notes($id);
        
        $this->load->view('header');
		$this->load->view('static_nav');
		$this->load->view('edit_notes_content',$data);
		$this->load->view('footer');
        
    }
     
    public function edit_ads()
    {
        $id=$this->input->post('ad_id');
        $this->load->model('my_ads');
        $data['records'] = $this->my_ads->get_edit_ad_record($id);
        
        $this->load->view('header');
		$this->load->view('static_nav');
		$this->load->view('edit_ads_content',$data);
		$this->load->view('footer');
        
    }
     
    public function deactive_ads()
    {
        $id = $this->input->post('ad_id'); 
        $this->load->model('my_ads');
         $data = array(  'active' => "0" );
        $this->my_ads->deactive_ad_record($id,$data);
       
        $this->my_ads();
        
    }

    public function active_ads()
    {
        $id = $this->input->post('ad_id'); 
        $this->load->model('my_ads');
         $data = array(  'active' => "1" );
        $this->my_ads->deactive_ad_record($id,$data);
       
        $this->my_ads();
        
    }
     public function deactive_notes()
    {
        $id = $this->input->post('notes_id'); 
        $this->load->model('upload_notes');
         $data = array(  'active' => "0" );
        $this->upload_notes->active_notes($id,$data);
       
        $this->my_notes();
        
    }
     public function active_notes()
    {
        $id = $this->input->post('notes_id'); 
        $this->load->model('upload_notes');
         $data = array(  'active' => "1" );
        $this->upload_notes->active_notes($id,$data);
       
        $this->my_notes();
        
    }
           
     
    public function delete_ads()
    {
      define('PUBPATH',str_replace(SELF,'',FCPATH)); // added
        
        $id = $this->input->post('ad_id');
        $this->delete_img($id,'img_1');
        $this->delete_img($id,'img_2');
        $this->delete_img($id,'img_3');
        $this->load->model('my_ads');
        $this->my_ads->delete_ad_record($id);
        
        $this->my_ads();
        
    } 
     public function delete_notes()
    {
      define('PUBPATH',str_replace(SELF,'',FCPATH));
        $id = $this->input->post('notes_id'); 
          $img_id = $this->input->post('softcopy'); 
        $this->load->model('upload_notes');
        $this->upload_notes->delete_notes_record($id);
       
         $path = $img_id;
         $path = substr($path,27);
        
         $path1 =PUBPATH.$path;
         $this->load->helper("url");
         unlink($path1);
        $this->my_notes();
        
    }
     
     public function update_name()
     {
         
         $this->load->model('sign_up');
         $this->sign_up->update_name($this->session->userdata('user_id'),$this->input->post('name'));
         redirect('/members/my_profile');
         
     }
     
     public function update_number()
     {
         
         $this->load->model('sign_up');
         $this->sign_up->update_number($this->session->userdata('user_id'),$this->input->post('number'));
         redirect('/members/my_profile');
         
     }
     
    public function update_dp()
    {
        
        if (!empty($_FILES['userfile1']['name'])) 
        {
           
            define('PUBPATH',str_replace(SELF,'',FCPATH)); // added 
            $config['upload_path'] = './Assets/img/Dp/';
            $config['allowed_types'] = 'gif|jpg|png';
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            $this->upload->set_allowed_types('*');
            $data['upload_data'] = '';
            if (!$this->upload->do_upload('userfile1')) {
                $data = array('msg' => $this->upload->display_errors());
            }
            else
            { //else, set the success message
                $data = array('msg' => "Upload success!");
                $data['upload_data'] = $this->upload->data();
               //getting file path 
                $file_name = '';
                $file_name .=base_url();
                $file_name .="/Assets/img/Dp/";
                $file_name .=$data['upload_data']['file_name'];
              // $this->session->set_userdata('dp', $file_name);

            }
            //$path = $this->session->userdata('dp');
            $this->load->model('sign_up');
            $id = $this->session->userdata('user_id');
            $path = $this->sign_up->image_path_to_delete($id);

            $path = substr($path,26);
            $path1 =PUBPATH.$path;
            $this->load->helper("url");
            unlink($path1);
            
           $this->sign_up->update_dp($file_name,$id);
            redirect('/members/my_profile');

        }  
    }
    
    public function update_ad()
    {
        if($this->is_logged_in())
        {
             $ad_id = $this->input->post('ad_id'); 
        
             $data = array(  'cat_id' => $this->input->post('cat'),
                            'ad_title' => $this->input->post('title'),
                            'price' => $this->input->post('price'),
                            'disc' => $this->input->post('disc'),
                            'keywords' => $this->input->post('keywords'),
                            'contact' => $this->input->post('contact'),
                           
                );
           
            
       
            if (!empty($_FILES['userfile1']['name'])) {
              $data['img_1'] = $this->update_image_upload('userfile1',$ad_id,'img_1');
            }  

            if (!empty($_FILES['userfile2']['name'])) {
              $data['img_2'] = $this->update_image_upload('userfile2',$ad_id,'img_2');
            }  
                              
            if (!empty($_FILES['userfile3']['name'])) {
              $data['img_3'] = $this->update_image_upload('userfile3',$ad_id,'img_3');
            }  
           
            $this->load->model('my_ads');
            $this->my_ads->update_ad_record($data,$ad_id);
            $this->my_ads();
        }
    }
     
     public function update_image_upload($name,$ad_id,$img_id)
     {
       
         
         define('PUBPATH',str_replace(SELF,'',FCPATH)); // added 
                $config['upload_path'] = './Assets/img/uploads/ads/';
                $config['allowed_types'] = 'gif|jpg|png';
                $this->load->library('upload', $config);
                $this->upload->initialize($config);
                $this->upload->set_allowed_types('*');
                $data['upload_data'] = '';
                if (!$this->upload->do_upload($name)) {
                    $data = array('msg' => $this->upload->display_errors());
                }
                else
                { //else, set the success message
                    $data = array('msg' => "Upload success!");
                    $data['upload_data'] = $this->upload->data();
                   //getting file path 
                    $file_name = '';
                    $file_name .=base_url();
                    $file_name .="/Assets/img/uploads/ads/";
                    $file_name .=$data['upload_data']['file_name'];
                  
                }
         $this->delete_img($ad_id,$img_id);
         return $file_name;
     }
     
     function delete_img($ad_id,$img_id)
     {
         
         $this->load->model('my_ads');
         $path = $this->my_ads->image_path_to_delete($ad_id,$img_id);
         if($path!='http://localhost/baichday//Assets/img/default.jpg')
         {
             $path = substr($path,27);
             $path1 =PUBPATH.$path;
             $this->load->helper("url");
             unlink($path1);
         }
          
         
     }
      public function update_note()
    {
        
        $notes_id = $this->input->post('notes_id'); 
         $data = array(  'user_id' => $this->session->userdata('user_id'),
                    'note_title' => $this->input->post('title'), 
                    'faculty' => $this->input->post('faculty'), 
                    'semester' =>$this->input->post('semester'), 
                    'author' => $this->input->post('teacher'),
                   
                    'disc'=>$this->input->post('discription') 
                           
                );
           
            
        if($this->is_logged_in())
        {
            if (!empty($_FILES['userfile']['name'])) {
              $data['softcopy'] = $this->update_note_upload('userfile',$notes_id,'softcopy');
            }  

            
           
            $this->load->model('upload_notes');
            $this->upload_notes->update_note_record($data,$notes_id);
            $this->my_notes();
        }
    }
     
     public function update_note_upload($name,$note_id,$img_id)
     {
       
         
         define('PUBPATH',str_replace(SELF,'',FCPATH)); // added 
                $config['upload_path'] = './Assets/img/uploads/notes/';
                $config['allowed_types'] = '*';
                $this->load->library('upload', $config);
                $this->upload->initialize($config);
                $this->upload->set_allowed_types('*');
                $data['upload_data'] = '';
                if (!$this->upload->do_upload($name)) {
                    $data = array('msg' => $this->upload->display_errors());
                }
                else
                { //else, set the success message
                    $data = array('msg' => "Upload success!");
                    $data['upload_data'] = $this->upload->data();
                   //getting file path 
                    $file_name = '';
                    $file_name .=base_url();
                    $file_name .="/Assets/img/uploads/notes/";
                    $file_name .=$data['upload_data']['file_name'];
                  
                }   
         
         $this->load->model('upload_notes');
         $path = $this->upload_notes->image_path_to_delete($note_id,$img_id);
         $path = substr($path,27);
         $path1 =PUBPATH.$path;
         $this->load->helper("url");
         unlink($path1);
          
         return $file_name;
     }
 
    public function my_Profile()
    {
        if($this->is_logged_in())
        {
            $email=$this->session->userdata('email');
            $this->load->model('sign_up');
        	 //getting data from db
            $data['records'] = $this->sign_up->user_data($email);
            
            
            $this->load->view('header');
    		$this->load->view('static_nav');
    		$this->load->view('my_profile_content',$data);
    		$this->load->view('footer');
        }
    }
     public function update_pass()
     {
         $old = $this->input->post('old');
         $new = $this->input->post('pass');
         $this->load->model('sign_up');
         $response=$this->sign_up->check_old_pass($old,$this->session->userdata('user_id'),$new);
         echo $response;
         
     }

    public function notes_detail($id)
    {
         
        $this->load->model('upload_notes');
        $this->load->model('commentModel');
        $data['records'] =  $this->upload_notes->get_note($id);
        $data['comments'] = $this->commentModel->getComments($id);
          
        $this->load->view('header');
        if($this->session->userdata('logged_in'))
        {
    	   $this->load->view('static_nav');
        }
        else
        { 
            $this->load->view('static_nav_lout');
        }
    	$this->load->view('notes_detail',$data); 
    	$this->load->view('footer');
 
    }
     
     public function download_increment()
     {
         
         $val=$this->input->post('value');
         $id=$this->input->post('id');
         $this->load->model('upload_notes');
         $this->upload_notes->download_increment($val,$id);
         echo $val;
     }
     
    public function insert_comment(){
        if(isset($_POST['task']) && $_POST['task']=="comment_insert"){

            $userId = (int) $this->input->post('userId');
            $comment = $this->input->post('comment');
            $adId = $this->input->post('adId');
            $name = $this->input->post('name');
            $comment = str_replace("\n", "<br>", $comment);

            $this->load->model('commentModel');
            $data['user_id'] = $userId;
            $data['text'] = $comment;
            $cmId = $this->commentModel->insert_com($data); 

            $dat['comment_id'] = $cmId;
            $dat['ad_id'] = $adId;
            $dat['likes'] = 0;
            $dat['not_seen'] = 1;

            $this->commentModel->insert_adCom($dat);

            $std = new stdClass();
            $std->userId = $userId;
            $std->comId  = $cmId;
            $std->comment = $comment;
            $std->userName = $name;
            $std->dp = $this->session->userdata('dp');

            echo json_encode($std);
        }
    }

      
    
 }